package com.example.myapplication.request

import androidx.lifecycle.MutableLiveData
import com.example.myapplication.bean.ListResultBean
import com.example.myapplication.bean.article.ArticleBean
import com.example.myapplication.bean.collect.OtherArticleResultBean
import com.example.myapplication.bean.collect.WebsiteCollectBean
import com.kun.base.net.Api
import com.kun.base.net.ResponseResult

/**
 *收藏相关的request
 *
 * @author : Kun
 * @date : 2021/6/7
 * @time : 17:43
 */
class CollectRequest {
    /**
     * 收藏文章接口返回的结果
     */
    val collectArticleResult = MutableLiveData<ResponseResult<ListResultBean<ArticleBean>>>()

    /**
     * 收藏网站返回的结果
     */
    val collectWebsiteResult = MutableLiveData<ResponseResult<List<WebsiteCollectBean>>>()

    /**
     * 新增收藏文章返回的结果
     */
    val addArticleResult = MutableLiveData<ResponseResult<Int>>()

    /**
     * 取消文章收藏的结果
     * {文章列表使用}
     */
    val delArticleResult = MutableLiveData<ResponseResult<Int>>()

    /**
     *  取消收藏文章的结果
     *  我的收藏页面
     */
    val delArticleResultForMe = MutableLiveData<ResponseResult<Int>>()

    /**
     * 删除收藏网站的结果
     */
    val deleteWebsiteResult = MutableLiveData<ResponseResult<Int>>()

    /**
     * 收藏站外文章
     */
    val addOtherArticleRequest = MutableLiveData<ResponseResult<OtherArticleResultBean>>()

    /**
     * 收藏网站
     */
    val addWebsiteResult = MutableLiveData<ResponseResult<WebsiteCollectBean>>()

    /**
     * 编辑收藏网站
     */
    val editWebsiteResult = MutableLiveData<ResponseResult<WebsiteCollectBean>>()

    /**
     * 获取收藏文章
     */
    suspend fun requestCollectArticle(index: Int) {
        collectArticleResult.value = Api.apiCall {
            Api.getInstance(IApi::class.java).getCollectArticleList(index)
        }
    }

    /**
     * 获取收藏网站
     */
    suspend fun requestWebsiteArticle() {
        collectWebsiteResult.value = Api.apiCall {
            Api.getInstance(IApi::class.java).getCollectWebsiteList()
        }
    }

    /**
     * 新增文章收藏
     */
    suspend fun requestAddCollectArticle(id: Int) {
        addArticleResult.value = Api.apiCall {
            Api.getInstance(IApi::class.java).collectArticle(id)
        }.apply { data = id }
    }

    /**
     * 取消新增文章收藏
     * {文章列表使用}
     */
    suspend fun requestDelCollectArticle(id: Int) {
        delArticleResult.value = Api.apiCall {
            Api.getInstance(IApi::class.java).unCollectArticle(id)
        }.apply { data = id }
    }

    /**
     * 取消收藏文章
     *  我的收藏页面
     */
    suspend fun requestDelCollectArticleForMe(id: Int, originId: Int) {
        val map = HashMap<String, Int>(1)
        map["originId"] = originId
        delArticleResultForMe.value = Api.apiCall {
            Api.getInstance(IApi::class.java).unCollectArticleForMe(id, map)
        }.apply { data = id }
    }

    /**
     * 删除收藏网站
     */
    suspend fun requestDeleteWebsite(id: Int) {
        deleteWebsiteResult.value = Api.apiCall {
            Api.getInstance(IApi::class.java).deleteWebsite(id)
        }.apply { data = id }
    }

    /**
     * 收藏站外文章
     * title，author，link
     */
    suspend fun requestAddOtherArticle(title: String, author: String, link: String) {
        val map = HashMap<String, String>(3)
        map["title"] = title
        map["author"] = author
        map["link"] = link
        addOtherArticleRequest.value = Api.apiCall {
            Api.getInstance(IApi::class.java).addOtherArticle(map)
        }
    }

    /**
     * 收藏网站
     */
    suspend fun requestAddWebsite(name: String, link: String) {
        val map = HashMap<String, String>(3)
        map["name"] = name
        map["link"] = link
        addWebsiteResult.value = Api.apiCall {
            Api.getInstance(IApi::class.java).addWebsite(map)
        }
    }
    /**
     * 编辑收藏网站
     */
    suspend fun requestEditWebsite(id:Int,name: String, link: String) {
        val map = HashMap<String, Any>(3)
        map["id"] = id
        map["name"] = name
        map["link"] = link
        editWebsiteResult.value = Api.apiCall {
            Api.getInstance(IApi::class.java).editWebsite(map)
        }
    }
}